getAggregation <- function(dataset,a,b,targetcat){
# expected input format:
# [1] "subject_name"    "ITEM_ID"         "LEFT_X"          "LEFT_Y"         
# [5] "RIGHT_X"         "RIGHT_Y"         "CONDITION"       "LEFT_ICA_EVENT" 
# [9] "RIGHT_ICA_EVENT" "itemOrder"       "CRIT_REG"        "SHIFT_LSV400" 

mbotheyes <- subset(melt(dataset, id=c("PARTICIPANT", "ITEM_ID","LEFT_X","LEFT_Y", "RIGHT_X", "RIGHT_Y", "ITEM_TYPE","CRIT_REG", "itemOrder")),!is.na(value))
mbotheyes$rawica <- atanh(mbotheyes$value)*3
ss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:10,12)],id=1:10, na.rm=TRUE)
summary(ss)
names(ss)[10] <- "eye"

css<-cast(ss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, mean)
css$rawica <-round(css$rawica*5)
css$it <- as.factor(ifelse(css$ITEM_TYPE==targetcat,0,1))
cssorig<-css

lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:10)],id=c(1,2,4:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$leftx<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, mean)$LEFT_X
minxcss<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, min)
maxxcss<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, max)
css$lxdiff <-maxxcss$LEFT_X - minxcss$LEFT_X
rm(lxss) 
rm(minxcss) 
rm(maxxcss)

lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:10)],id=c(1:3,5:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$lefty<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, mean)$LEFT_Y
minycss<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, min)
maxycss<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, max)
css$lydiff <-maxycss$LEFT_Y - minycss$LEFT_Y
rm(lxss)
rm(minycss)
rm(maxycss)

lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:10)],id=c(1:4,6:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$rightx<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, mean)$RIGHT_X
mini<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, min)
maxi<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, max)
css$rxdiff <-maxi$RIGHT_X - mini$RIGHT_X
rm(lxss)
rm(mini)
rm(maxi)

lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:10)],id=c(1:5,7:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$righty<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, mean)$RIGHT_Y
mini<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, min)
maxi<-cast(lxss, PARTICIPANT + ITEM_TYPE + ITEM_ID + itemOrder+eye ~
variable, max)
css$rydiff <-maxi$RIGHT_Y - mini$RIGHT_Y
#cssorig$xdiff <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lxdiff, css$rxdiff))
#cssorig$ydiff <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lydiff, css$rydiff))
#cssorig$xpos <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$leftx, css$rightx))
#cssorig$ypos <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lefty, css$righty))

cssorig$xdiff <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lxdiff, css$rxdiff))
cssorig$ydiff <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lydiff, css$rydiff))
cssorig$xpos <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$leftx, css$rightx))
cssorig$ypos <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lefty, css$righty))

return(cssorig)
}

getAggregationDual <- function(dataset,a,b,targetcat){
# expected input format:
#  [1] "PARTICIPANT"     "ITEM_ID"         "LEFT_X"          "LEFT_Y"          "RIGHT_X"         "RIGHT_Y"         "ITEM_TYPE"       "LEFT_ICA_EVENT" 
# [9] "RIGHT_ICA_EVENT" "itemOrder"       "CRIT_REG"       
mbotheyes <- subset(melt(dataset, id=c("subject_name", "ITEM_ID","LEFT_X","LEFT_Y", "RIGHT_X", "RIGHT_Y", "CONDITION","CRIT_REG", "itemOrder", "SHIFT_LSV400")),!is.na(value))
mbotheyes$rawica <- atanh(mbotheyes$value)*3
ss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:11,13)],id=1:11, na.rm=TRUE)
summary(ss)
names(ss)[11] <- "eye"

css<-cast(ss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, mean)
css$rawica <-round(css$rawica*5)
css$cond <- as.factor(ifelse(css$CONDITION==targetcat,0,1))
cssorig<-css



# get average lateral steering deviation
lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:11)],id=c(1:9,11), na.rm=TRUE)
summary(lxss)
names(lxss)[10]<-"eye"
cssorig$lsv_400<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, mean)$SHIFT_LSV400


# get average fixation positions and distances of viewing locations
lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:11)],id=c(1:2,4:11), na.rm=TRUE)
summary(lxss)
names(lxss)[10]<-"eye"
css$leftx<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, mean)$LEFT_X
minxcss<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, min)
maxxcss<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, max)
css$lxdiff <-maxxcss$LEFT_X - minxcss$LEFT_X
rm(lxss) 
rm(minxcss) 
rm(maxxcss)

lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:11)],id=c(1:3,5:11), na.rm=TRUE)
summary(lxss)
names(lxss)[10]<-"eye"
css$lefty<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, mean)$LEFT_Y
minycss<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, min)
maxycss<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, max)
css$lydiff <-maxycss$LEFT_Y - minycss$LEFT_Y
rm(lxss)
rm(minycss)
rm(maxycss)

lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:11)],id=c(1:4,6:11), na.rm=TRUE)
summary(lxss)
names(lxss)[10]<-"eye"
css$rightx<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, mean)$RIGHT_X
mini<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, min)
maxi<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, max)
css$rxdiff <-maxi$RIGHT_X - mini$RIGHT_X
rm(lxss)
rm(mini)
rm(maxi)

lxss <-melt(subset(mbotheyes, CRIT_REG>a & CRIT_REG<b)[,c(1:11)],id=c(1:5,7:11), na.rm=TRUE)
summary(lxss)
names(lxss)[10]<-"eye"
css$righty<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, mean)$RIGHT_Y
mini<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, min)
maxi<-cast(lxss, subject_name + CONDITION + ITEM_ID + itemOrder+eye ~
variable, max)
css$rydiff <-maxi$RIGHT_Y - mini$RIGHT_Y
#cssorig$xdiff <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lxdiff, css$rxdiff))
#cssorig$ydiff <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lydiff, css$rydiff))
#cssorig$xpos <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$leftx, css$rightx))
#cssorig$ypos <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lefty, css$righty))

cssorig$xdiff <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lxdiff, css$rxdiff))
cssorig$ydiff <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lydiff, css$rydiff))
cssorig$xpos <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$leftx, css$rightx))
cssorig$ypos <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lefty, css$righty))

return(cssorig)
}

getAggregationVW <- function(dataset,a,b){
# expected input format:
# [1] "participant"     "item"            "LEFT_X"          "LEFT_Y"         
# [5] "RIGHT_X"         "RIGHT_Y"         "concessive"      "LEFT_ICA_EVENT" 
# [9] "RIGHT_ICA_EVENT" "itemOrder"       "rCRIT_REG"      
mbotheyes <- subset(melt(dataset, id=c("participant", "item","LEFT_X","LEFT_Y", "RIGHT_X", "RIGHT_Y", "concessive","rCRIT_REG", "itemOrder")),!is.na(value))
mbotheyes$rawica <- atanh(mbotheyes$value)*3
ss <-melt(subset(mbotheyes, rCRIT_REG>a & rCRIT_REG<b)[,c(1:10,12)],id=1:10, na.rm=TRUE)
summary(ss)
names(ss)[10] <- "eye"

css<-cast(ss, participant + concessive + item + itemOrder+eye ~
variable, mean)
css$rawica <-round(css$rawica*5)
cssorig<-css



# get average fixation positions and distances of viewing locations
lxss <-melt(subset(mbotheyes, rCRIT_REG>a & rCRIT_REG<b)[,c(1:10)],id=c(1:2,4:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$leftx<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, mean)$LEFT_X
minxcss<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, min)
maxxcss<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, max)
css$lxdiff <-maxxcss$LEFT_X - minxcss$LEFT_X
rm(lxss) 
rm(minxcss) 
rm(maxxcss)

lxss <-melt(subset(mbotheyes, rCRIT_REG>a & rCRIT_REG<b)[,c(1:10)],id=c(1:3,5:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$lefty<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, mean)$LEFT_Y
minycss<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, min)
maxycss<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, max)
css$lydiff <-maxycss$LEFT_Y - minycss$LEFT_Y
rm(lxss)
rm(minycss)
rm(maxycss)

lxss <-melt(subset(mbotheyes, rCRIT_REG>a & rCRIT_REG<b)[,c(1:10)],id=c(1:4,6:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$rightx<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, mean)$RIGHT_X
mini<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, min)
maxi<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, max)
css$rxdiff <-maxi$RIGHT_X - mini$RIGHT_X
rm(lxss)
rm(mini)
rm(maxi)

lxss <-melt(subset(mbotheyes, rCRIT_REG>a & rCRIT_REG<b)[,c(1:10)],id=c(1:5,7:10), na.rm=TRUE)
summary(lxss)
names(lxss)[9]<-"eye"
css$righty<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, mean)$RIGHT_Y
mini<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, min)
maxi<-cast(lxss, participant + concessive + item + itemOrder+eye ~
variable, max)
css$rydiff <-maxi$RIGHT_Y - mini$RIGHT_Y
#cssorig$xdiff <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lxdiff, css$rxdiff))
#cssorig$ydiff <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lydiff, css$rydiff))
#cssorig$xpos <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$leftx, css$rightx))
#cssorig$ypos <- scale(ifelse(css$eye=="LEFT_ICA_EVENT", css$lefty, css$righty))

cssorig$xdiff <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lxdiff, css$rxdiff))
cssorig$ydiff <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lydiff, css$rydiff))
cssorig$xpos <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$leftx, css$rightx))
cssorig$ypos <-(ifelse(css$eye=="LEFT_ICA_EVENT", css$lefty, css$righty))

return(cssorig)
}

